--[[
	Ikarus Unit-Frames Addon	
	released under GNU General Public License version 3 (GPLv3) 
	by McGaerty & Sl4ker
	
	contact: mcgaerty@ikarus-systems.net rom@sl4ker.de	
	download: http://rom.curse.com/downloads/rom-addons/details/ikarus-unitframes.aspx
	
	special thanks to Qwox and novayuna for inspiration and stable basics
]]



	--[[
	
		In Addition to IUC, Ikarus supports automatic color selection via the colorizer 
		property. A colorizer can be applied to almost every object that can be colored,
		e.g. statusbars, labels...
		
		
		The following colorizers are available by now:
		
		DIFFICULTY			Uses the RoM difficulty table to select color (depends on lvl difference)
		RELATION				Uses the RoM relation table (color depending on how "friendly" the unit is)
		BLOODBAR				Fades the color from green to red depending on relative health
		CLASS				Color selection depending on RoM system class color
		WOW_CLASS			Same as CLASS, but with WoW colors
	]]
	
ikarus.colorizers.items = {};
ikarus.colorizers.isInitialised = false;
	
function ikarus.colorizers:IsInitialised()
	return self.isInitialised == true;
end;
	
function ikarus.colorizers:Initialise()
	if not(self:IsInitialised()) then
		self:RegisterDipper("DIFFICULTY", ikarus.colorizers.DIFFICULTY);
		self:RegisterDipper("RELATION", ikarus.colorizers.RELATION);
		self:RegisterDipper("BLOODBAR", ikarus.colorizers.BLOODBAR);
		self:RegisterDipper("CLASS", ikarus.colorizers.CLASS);
		self:RegisterDipper("WOW_CLASS", ikarus.colorizers.WOW_CLASS);
	end;
end;

function ikarus.colorizers:RegisterDipper(name, item)
	if(name ~= nil and item ~= nil) then
		self.items[name] = item;
	end;
end;

function ikarus.colorizers:Colorize(name, delegate)
	local item = self.items[name];
	if(item) then
		return item(delegate);
	end;
end;


function ikarus.colorizers.DIFFICULTY(delegate)
	if(delegate:IsUnit()) then
		if(UnitCanAttack("player", delegate.unit))then
			diffLevel = UnitLevel(delegate.unit) - UnitLevel("player");
			if ( diffLevel >= 7 ) then
				return ikarus.constants.colors.DIFFICULTY[1];
			elseif ( diffLevel >= 4 ) then
				return ikarus.constants.colors.DIFFICULTY[2];
			elseif ( diffLevel >= -3 ) then
				return ikarus.constants.colors.DIFFICULTY[3];
			elseif ( diffLevel >= -6 ) then
				return ikarus.constants.colors.DIFFICULTY[4];
			elseif ( diffLevel >= -9 ) then
				return ikarus.constants.colors.DIFFICULTY[5];
			else
				return ikarus.constants.colors.DIFFICULTY[6];
			end;
		end;
	end;
end;

function ikarus.colorizers.RELATION(delegate)
	if(delegate:IsUnit()) then
		if(UnitCanAttack(delegate.unit, "player")) then
			if(UnitCanAttack("player", delegate.unit)) then
				return ikarus.constants.colors.RELATION[1];
			else
				return ikarus.constants.colors.RELATION[2];
			end;
		elseif(UnitCanAttack("player", delegate.unit)) then
			return ikarus.constants.colors.RELATION[3];
		elseif(UnitInParty(delegate.unit)) then
			return ikarus.constants.colors.RELATION[5];
		else
			return ikarus.constants.colors.RELATION[4];
		end;
	end;
end;

--  Special thx to p.b. a.k.a. novayuna for the original bloodbar fade function ;)
function ikarus.colorizers.BLOODBAR(delegate)
	if(delegate:IsUnit()) then
		local healthRatio = 0;
		if(UnitIsPlayer(delegate.unit)) then
			local hp, hpMax = UnitHealth(delegate.unit), UnitMaxHealth(delegate.unit);
			if(hpMax and hpMax > 0) then
				healthRatio = 100 / hpMax * hp;
			end;
		else
			healthRatio = tonumber(UnitHealth(delegate.unit));
		end;
		return ikarus.utils:HSBtoRGB(math.ceil((healthRatio / 100) * 127.5), 100, 100);
	end;
end;

function ikarus.colorizers.CLASS(delegate)
	if(delegate:IsUnit()) then
		local class,_ = UnitClassToken(delegate.unit);
		return ikarus.constants.colors.CLASS[class];
	end;
end;

function ikarus.colorizers.WOW_CLASS(delegate)
	if(delegate:IsUnit()) then
		local class,_ = UnitClassToken(delegate.unit);
		return ikarus.constants.colors.WOW[class];
	end;
end;